package com.to8to.scene.stat;

import static com.to8to.scene.server.Commons.mongo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.to8to.scene.server.Commons;
import com.to8to.scene.server.Tables;

public class RefreshAllCount
{

    public static Logger logger = LoggerFactory.getLogger(RefreshAllCount.class);

    static StatSceneUpdateEngine sceneUpdate = new StatSceneUpdateEngine();
    static StatDiaryUpdateEngine diaryUpdate = new StatDiaryUpdateEngine();

    public static void refreshScene(boolean with_pause) throws InterruptedException
    {
        DBObject fields = new BasicDBObject("_id", 1);
        DBCursor cursor = mongo.cursor(Tables.Scene, null, fields, null, 0, 0);
        logger.debug("刷新现场信息开始-------------");
        int i = 0;
        for (DBObject dbObject : cursor)
        {
            Object _id_obj = dbObject.get("_id");
            if (!(_id_obj instanceof String))
                continue;
            String sceneId = (String) _id_obj;
            logger.debug("update scene:{}", sceneId);
            refreshScene(sceneId);
            i++;
            if (i % 100 == 0)
            {
                logger.info("已刷新现场数：" + i);
            }
            if (with_pause)
                Thread.sleep(1000);
        }
    }

    public static void refreshScene(String sceneId)
    {
        sceneUpdate.refreshDiaryInfo(sceneId, 0, null, true);
        sceneUpdate.refreshCommentInfo(sceneId, 0, null, false, true);
        sceneUpdate.refreshProductInfo(sceneId, 0, null, false);
    }

    public static void refreshDiary(boolean with_pause) throws InterruptedException
    {
        DBObject fields = new BasicDBObject("_id", 1);
        DBCursor cursor = mongo.cursor(Tables.Diary, null, fields, null, 0, 0);
        logger.debug("刷新日记信息开始-------------");
        int i = 0;
        for (DBObject dbObject : cursor)
        {
			Object _id_obj = dbObject.get("_id");
            if (!(_id_obj instanceof String))
                continue;
            String diaryId = (String) _id_obj;
            refreshDiary(diaryId);
            i++;
            if (i % 100 == 0)
            {
                logger.debug("已刷日记数：" + i);
            }
            if (with_pause)
                Thread.sleep(1000);
        }
    }

    public static void refreshDiary(String diaryId)
    {
        diaryUpdate.refreshDiaryInfo(diaryId, 0, null, false);
        diaryUpdate.refreshCommentInfo(diaryId, 0, null, false, true);
        diaryUpdate.refreshProductInfo(diaryId, 0, null, false);
    }

    public static void main(String[] args) throws InterruptedException
    {
        Commons.init(null);

        long start = System.currentTimeMillis();
        //                List<TScene> scenes = ThriftUtil.listCursor(mongo.cursor(Tables.Scene, new BasicDBObject("com_id", 573608), null, null, 0, 0), TScene.class);
        //                for (TScene scene : scenes)
        //        List<String> scene_ids = Arrays.asList(new String[] { "57039", "61579", "23883", "295449", "285493" });
        //        for (String scene_id : scene_ids)
        refreshScene("61579");
//        refreshDiary("391");
//        refreshDiary(false);
        long end = System.currentTimeMillis();
        logger.debug("刷新耗时：" + (end - start) + "毫秒");
        System.exit(0);
    }
}
